class SoundManager {
	public constructor() {
	}

	private static _instance:SoundManager;
	public static instance():SoundManager{
		if(!SoundManager._instance){
			SoundManager._instance = new SoundManager();
		}
		return SoundManager._instance;
	}


	private _channel:egret.SoundChannel;
	private _crtBGM:string = '';
	private _isPlay:boolean = false;
	private _swBGM:boolean = true;
	public get isBGMPlay(){
		return this._swBGM;
	}
	private _swAudio:boolean = true;
	public get isAudioPlay(){
		return this._swAudio;
	}

	public init(){
		
	}

	public swBGM(){
		this._swBGM = !this._swBGM;
		if(this._channel){
			// this._channel.volume = this._swBGM?1:0;
			this._swBGM?this.goOn():this.stop();
		}
		return this._swBGM;
	}

	public swAudio(){
		this._swAudio = !this._swAudio;
		return this._swAudio;
	}

	public goOn(){
		if(this._crtBGM != ''){
			this.play(this._crtBGM);
		}
	}

	public play(soundName:string, type:SoundType = SoundType.BGM,loops = 1){
		let sound:egret.Sound = RES.getRes(soundName);
		if(sound){
			if(type == SoundType.BGM){
				if(this._crtBGM != soundName || !this._isPlay){
					this.stop();
					if(this._swBGM){
						this._isPlay = true;
						this._crtBGM = soundName;
						this._channel = sound.play();
					}
				}
			}else if(type == SoundType.AUDIO){
				if(this._swAudio){
					sound.play(0, 1);
				}
			}
		}
	}

	public stop(){
		if(this._channel && this._isPlay){
			this._channel.stop();
			this._isPlay = false;
		}
	}

	/**
	 * 播放闯关模式背景音乐
	 */
	public playCustomsSound(){
		// this.play("level_mp3", SoundType.BGM);
		// this.playMusic('level_mp3');
		TTSoundManager.instance().playSound('level');
	}

	/**
	 * 播放无尽游戏场景音乐
	 */
	public playEndLessSound(){
		// this.play("endless_bgm_mp3", SoundType.BGM);
		// this.playMusic('endless_bgm_mp3');
		TTSoundManager.instance().playSound('endless_bgm');
	}

	/**
	 * 播放刀挥打音效
	 */
	public playCutLostSound(){
		// this.play("cut_mp3", SoundType.AUDIO);
		// this.playSound('cut_mp3');
		TTSoundManager.instance().playAudio('cut');
	}

	/**
	 * 播放水果碎裂音效
	 */
	public playImpactSound(){
		// this.play("cuted_mp3", SoundType.AUDIO);
		// this.playSound('cuted_mp3');
		TTSoundManager.instance().playAudio('cuted');
	}

	public playMusic(name)
	{
		// RES.getResAsync(name, ()=>{
		// 	this.play(name, SoundType.BGM);
		// },this);
		TTSoundManager.instance().playSound(name);
	}

	/**
	 * 播放音效
	 */
	public playSound(name, loops = 1){
		// RES.getResAsync(name, ()=>{
		// this.play(name, SoundType.AUDIO, loops);
		// },this);
		TTSoundManager.instance().playAudio(name);
	}
}

enum SoundType{
	BGM,//背景音乐
	AUDIO//音效
}
